<?php


namespace App\backend\services;


use App\backend\models\MemberModel;
use App\common\models\BaseModel;
use App\Exceptions\InvalidRequestException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class MemberServices
{

    /**
     * 会员列表
     */
    public function list($request)
    {
        return MemberModel::query()
        // 关键字搜索
        ->when($keyword = $request['keyword'], function ($query) use ($keyword) {
            return $query->where(function ($query) use ($keyword) {
                return $query->orWhere('mobile', 'like', '%' . $keyword . '%')
                    ->orWhere('nickname', 'like', '%' . $keyword . '%')
                    ->orWhere('realname', 'like', '%' . $keyword . '%');
            });
        })
        // 级别搜索
        ->when($level = $request['level'], function ($query) use ($level) {
            $query->where('level', $level);
        })
        // 注册日期搜索
        ->when($start_time = $request['start_time'],$end_time = $request['end_time'], function ($query) use ($start_time,$end_time) {
            $query->whereBetween('created_at', [strtotime($start_time),strtotime($end_time)]);
        })
        ->paginate($request->limit??10);
    }

    /**
     * 会员详情
     *
     */
    public static function getDetails($request)
    {
        if (!$data = MemberModel::query()->where('uid',$request['uid'])->first()){
            throw new InvalidRequestException(10009);
        }
        return $data;
    }

    /**
     * 添加会员
     */
    public function addMember($request)
    {
        if ($request['confirm_password'] != $request['password']){
            throw new InvalidRequestException(20014);
        }
        if (MemberModel::query()->where('mobile',$request['mobile'])->first()){
            throw new InvalidRequestException(10016);
        }
        $data = [
            'mobile' => $request['mobile'], // 手机号码
            'email' => $request['email'], // 邮箱
            'password' => $request['password'],
            'level_id' => $request['level_id'],
            'group_id' => $request['group_id'],
            'type' => $request['type'],
            'points' => $request['points'],
            'balance' => $request['balance'],
            'avatar' => $request['avatar'],
            'nickname' => $request['nickname'],
            'realname' => $request['realname'],
            'birthday' => $request['birthday'],
            'bio' => $request['bio'],
        ];
        return MemberModel::query()->insert($data);
    }

    /**
     * 编辑会员
     */
    public function editMember($request)
    {
        if (!MemberModel::query()->where('uid',$request['uid'])->first()){
            throw new InvalidRequestException(10009);
        }
        if ($request['confirm_password'] != $request['password']){
            throw new InvalidRequestException(20014);
        }
        $data = [
            'mobile' => $request['mobile'], // 手机号码
            'email' => $request['email'], // 邮箱
            'password' => $request['password'],
            'level_id' => $request['level_id'],
            'group_id' => $request['group_id'],
            'type' => $request['type'],
            'points' => $request['points'],
            'balance' => $request['balance'],
            'avatar' => $request['avatar'],
            'nickname' => $request['nickname'],
            'realname' => $request['realname'],
            'birthday' => $request['birthday'],
            'bio' => $request['bio'],
            'updated_at' => time(),
        ];
        return MemberModel::query()->where('uid',$request['uid'])->update($data);
    }

    /**
     * 删除会员
     */
    public function deleteMember($request)
    {
        if (!MemberModel::query()->where('uid',$request['uid'])->first()){
            throw new InvalidRequestException(10009);
        }
        return MemberModel::query()->where('uid',$request['uid'])->delete();
    }

}
